<?php namespace Cms\Api;

use Cms\Model\Field as FieldModel;

class Field
{
	protected $db;
	
	protected $error = '未知错误';

	public function __construct()
	{
		$this->db = new FieldModel;
	}

	//获取所有模型
	public function getAll($mid)
	{
		return Db::table('field')->where('mid',$mid)->orderBy("fieldsort","asc")->get();
	}

	//添加模型
	public function store()
	{
		if($this->db->create())
		{
			if($this->db->add())
			{
				return true;
			}
		}
	}

	//获取字段信息
	public function getFieldData($fid)
	{
		$field = Db::table('field')->where('fid',$fid)->first();
		$field['set'] = unserialize($field['set']);
		$field['model_name']=Db::table('model')->where("mid",$field['mid'])->pluck('model_name');
		return $field;
	}

	//更新模型
	public function update()
	{
		if($this->db->create())
		{
			if($this->db->save())
			{
				return true;
			}
		}
	}

	//删除模型
	public function destroy($fid)
	{
		return $this->db->delete($fid);
	}

	//获取错误
	public function getError()
	{
		return $this->db->getError()?:$this->error();
	}
}